package com.example.demo.service.impl;

import com.example.demo.dao.TbMoodDao;
import com.example.demo.entity.TbBlogEntity;
import com.example.demo.entity.TbMoodEntity;
import com.example.demo.service.TbMoodService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class TbMoodServiceImpl implements TbMoodService {
    final TbMoodDao moodDao;
    @Autowired
    public TbMoodServiceImpl(TbMoodDao moodDao) {
        this.moodDao = moodDao;
    }
    @Override
    public boolean pushMood(String content) {
        TbMoodEntity moodEntity = new TbMoodEntity();
        moodEntity.setMoodContent(content);
        moodEntity.setMoodDatetime(new Timestamp(System.currentTimeMillis()));
        moodEntity.setMoodUp(0);
        moodDao.save(moodEntity);
        return true;
    }

    @Override
    public Map<String, Object> getMoodsByPage(Integer index, Integer size) {
        Sort sort = Sort.by(Sort.Order.desc("moodDatetime"));
        if(index <= 0) {
            index = 1;
        }
        if(size <= 0) {
            size = 10;
        }
        Pageable pageable = PageRequest.of(index - 1, size, sort);
        Page<TbMoodEntity> moodPage = moodDao.findAll(pageable);
        Map<String, Object> result = new HashMap<>();
        result.put("result", moodPage.getContent());
        result.put("total", moodPage.getTotalElements());
        return result;
    }

    public void deleteMoodById(int [] idList) {
        for (int id : idList) {
            moodDao.deleteById(id);
        }
    }
}
